package www.tvbigo.cn.xiaoxuan.widget;

/******************************************************
 *
 ***********本工程的源于www.tvbigo.cn开发社区***************
 *************我们的开发QQ交流群：257251953****************
 ****我们的宗旨是服务广大Android/TVOS开发者 欢迎大家加入我们******
 ***本源码仅用于学习 如有商用请联系我们 一经发现我们有追究其责任权利***
 *
 ******************************************************/
import www.tvbigo.cn.xiaoxuan.R;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.FocusFinder;
import android.view.KeyEvent;
import android.view.View;
import android.widget.HorizontalScrollView;

/**
 * 
 * @Description 自定义横滚的Scrollview
 * @author Joychang E-mail: changwanjob@163.com
 * @version 创建时间：2015年12月7日 下午12:09:02
 */
public class SmoothHorizontalScrollView extends HorizontalScrollView
{
    final String TAG = "SmoothHorizontalScrollView";
    
    public SmoothHorizontalScrollView(Context context)
    {
        this(context, null, 0);
    }
    
    public SmoothHorizontalScrollView(Context context, AttributeSet attrs)
    {
        this(context, attrs, 0);
    }
    
    public SmoothHorizontalScrollView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
    }
    
    @Override
    public void scrollBy(int dx, int dy)
    {
        super.scrollBy(dx, dy);
    }
    
    @Override
    public void computeScroll()
    {
        super.computeScroll();
    }
    
    public void setTabIndex(int index)
    {
        // ...
    }
    
    public void setTabCount(int _count)
    {
        // ...
    }
    
    @Override
    public boolean dispatchKeyEvent(KeyEvent event)
    {
        View currentFocused = findFocus();
        if (currentFocused == this)
            currentFocused = null;
        if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT && event.getAction() == KeyEvent.ACTION_DOWN)
        {
            // View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, View.FOCUS_LEFT);
        }
        else if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT && event.getAction() == KeyEvent.ACTION_DOWN)
        {
            View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, View.FOCUS_RIGHT);
            if (nextFocused == null)
            {
                Log.d(TAG, "no move to right");
            }
        }
        
        boolean ret = super.dispatchKeyEvent(event);
        if (ret == true
            && (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT && event.getAction() == KeyEvent.ACTION_DOWN || event.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT
                && event.getAction() == KeyEvent.ACTION_DOWN))
        {
            // ...
        }
        return ret;
    }
    
    @Override
    protected int computeScrollDeltaToGetChildRectOnScreen(Rect rect)
    {
        if (getChildCount() == 0)
            return 0;
        
        int width = getWidth();
        int screenLeft = getScrollX();
        int screenRight = screenLeft + width;
        
        int fadingEdge = this.getResources().getDimensionPixelSize(R.dimen.fading_edge);
        
        // leave room for left fading edge as long as rect isn't at very left
        if (rect.left > 0)
        {
            screenLeft += fadingEdge;
        }
        
        // leave room for right fading edge as long as rect isn't at very right
        if (rect.right < getChildAt(0).getWidth())
        {
            screenRight -= fadingEdge;
        }
        
        int scrollXDelta = 0;
        
        if (rect.right > screenRight && rect.left > screenLeft)
        {
            // need to move right to get it in view: move right just enough so
            // that the entire rectangle is in view (or at least the first
            // screen size chunk).
            
            if (rect.width() > width)
            {
                // just enough to get screen size chunk on
                scrollXDelta += (rect.left - screenLeft);
            }
            else
            {
                // get entire rect at right of screen
                scrollXDelta += (rect.right - screenRight);
            }
            
            // make sure we aren't scrolling beyond the end of our content
            int right = getChildAt(0).getRight();
            int distanceToRight = right - screenRight;
            scrollXDelta = Math.min(scrollXDelta, distanceToRight);
            
        }
        else if (rect.left < screenLeft && rect.right < screenRight)
        {
            // need to move right to get it in view: move right just enough so that
            // entire rectangle is in view (or at least the first screen
            // size chunk of it).
            
            if (rect.width() > width)
            {
                // screen size chunk
                scrollXDelta -= (screenRight - rect.right);
            }
            else
            {
                // entire rect at left
                scrollXDelta -= (screenLeft - rect.left);
            }
            
            // make sure we aren't scrolling any further than the left our content
            scrollXDelta = Math.max(scrollXDelta, -getScrollX());
        }
        return scrollXDelta;
    }
    
    @Override
    protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect)
    {
        
        // convert from forward / backward notation to up / down / left / right
        // (ugh).
        
        if (previouslyFocusedRect != null)
        {
            if (direction == View.FOCUS_FORWARD)
            {
                direction = View.FOCUS_RIGHT;
            }
            else if (direction == View.FOCUS_BACKWARD)
            {
                direction = View.FOCUS_LEFT;
            }
            View nextFocus = FocusFinder.getInstance().findNextFocusFromRect(this, previouslyFocusedRect, direction);
            if (nextFocus == null)
            {
                return false;
            }
            return nextFocus.requestFocus(direction, previouslyFocusedRect);
        }
        else
        {
            int index;
            int increment;
            int end;
            int count = this.getChildCount();
            if ((direction & FOCUS_FORWARD) != 0)
            {
                index = 0;
                increment = 1;
                end = count;
            }
            else
            {
                index = count - 1;
                increment = -1;
                end = -1;
            }
            for (int i = index; i != end; i += increment)
            {
                View child = this.getChildAt(i);
                if (child.getVisibility() == View.VISIBLE)
                {
                    if (child.requestFocus(direction, previouslyFocusedRect))
                    {
                        return true;
                    }
                }
            }
            return false;
        }
    }
}
